package com.example.permissionservice.service;

import com.example.permissionservice.dao.RoleMapper;
import com.example.permissionservice.dao.RoleResourceRelationMapper;
import com.example.permissionservice.pojo.Role;
import com.example.permissionservice.pojo.RoleExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleResourceRelationMapper roleResourceRelationMapper;

    public long countByExample(RoleExample example){
        return roleMapper.countByExample(example);
    }

    public int deleteLotsByPrimaryKey(List<Integer> idList) {
        return roleMapper.deleteLotsByPrimaryKey(idList);
    }

    public int insertSelective(Role record){
        return roleMapper.insertSelective(record);
    }

    public List<Role> selectAllRole(){
        return roleMapper.selectByExample(null);
    }

    public List<Role> selectByExample(RoleExample example){
        return roleMapper.selectByExampleWithResource(example);
    }

    public Role selectByPrimaryKey(Integer id){
        return roleMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Role record) {
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 该方法会通过角色名检查该角色是否存在
     * @return 该用户是否存在的Boolean
     */
    public Boolean checkRoleName(String roleName){
        long iResult = 0;

        try{
            RoleExample example = new RoleExample();
            example.createCriteria().andRole_nameLike(roleName);
            iResult = roleMapper.countByExample(example);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Boolean bResult = iResult >= 1;//大于1则说明该用户名存在

        return bResult;
    }

    /**
     * 该方法会通过用户名检查该角色的所有属性是否合法，用于新建角色
     * @return 携带错误信息的map，若map.size()>0则说明验证失败
     */
    public Map<String,Object> checkCreateRole(Role role, BindingResult result){
        Map<String , Object> errorMap = new HashMap<>();

        //先检查数据格式是否有误
        if (result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError error : fieldErrors){
                errorMap.put(error.getField(),error.getDefaultMessage());
            }
        }
        //再检查该角色名是否已存在
        if (this.checkRoleName(role.getRole_name())){
            errorMap.put("role_name","该用户名已存在!");
        }
        //返回errorMap
        return  errorMap;
    }

    /**
     * 该方法会通过用户名检查该角色的所有属性是否合法，用于修改角色
     * @return 携带错误信息的map，若map.size()>0则说明验证失败
     */
    public Map<String,Object> checkUpdateRole(Role role, BindingResult result){
        Map<String , Object> errorMap = new HashMap<>();

        //先检查数据格式是否有误
        if (result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError error : fieldErrors){
                errorMap.put(error.getField(),error.getDefaultMessage());
            }
        }
        //返回errorMap
        return  errorMap;
    }

    public int deleteByPrimaryKey(Integer role_id) {
        return roleMapper.deleteByPrimaryKey(role_id);
    }
}
